﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace Brain.Editor
{
    public partial class SceneOptionsForm : Form
    {
        public SceneOptionsForm()
        {
            InitializeComponent();

            this.groupBox1.Enabled = !string.IsNullOrEmpty(Engine.Instance.SerializationManager.SceneData.RootFolder);
            if (!this.groupBox1.Enabled)
            {
                MessageBox.Show("You can only change options after you save you Scene");
            }
        }

        private void UpdatePluginsList()
        {
            this.list_Plugins.Items.Clear();

            IEditorPluginManager p = Engine.Instance.EditorManager.PluginManager;

            for (int i = 0; i < p.PluginAssemblies.Count; i++)
            {
                Assembly ass = p.PluginAssemblies[i];
                this.list_Plugins.Items.Add(ass);
            }

            this.panel_Plugins.Controls.Clear();

            for (int i = 0; i < p.PluginTypes.Count; i++)
            {
                LoadPlugin plugin = p.PluginTypes[i];

                LabelCheckBox box = new LabelCheckBox(plugin.PluginType.Name);
                box.Location = new Point(10, i * 25);
                box.Tag = plugin;
                this.panel_Plugins.Controls.Add(box);
            }
        }

        private void button_AddPlugin_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog open = new OpenFileDialog())
            {
                open.Filter = "Plugin Files|*.dll|Brain 2 Plugin Files|*.bpl";

                if (open.ShowDialog() == DialogResult.OK)
                {
                    string root = Engine.Instance.SerializationManager.SceneData.RootFolder;
                    string plugins = Path.Combine(root, "Plugins");
                    if (!Directory.Exists(plugins))
                    {
                        Directory.CreateDirectory(plugins);
                    }
                    FileInfo file = new FileInfo(open.FileName);

                    string newFilePath = Path.Combine(plugins, file.Name);
                    FileInfo futureFile = new FileInfo(newFilePath);
                    if (futureFile.FullName != file.FullName && !futureFile.Exists)
                    {
                        FileInfo newFile = file.CopyTo(newFilePath);
                        Engine.Instance.EditorManager.PluginManager.HandlePlugin(newFile);
                    }
                    else
                    {
                        Engine.Instance.EditorManager.PluginManager.HandlePlugin(futureFile);
                    }

                    UpdatePluginsList();
                }
            }
        }

        private void button_Apply_Click(object sender, EventArgs e)
        {
            string message = @"Plugins applied CANNOT be unapplied. The only way to remove a plugin 
is to close the scene and load it again(if you didn't save with the plugins). Plugins create managers, which may be linked to objects on the Engine, making it impossible to unload. 
Do you really want to apply the plugins you selected?";
            if (MessageBox.Show(message, "", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                for (int i = 0; i < this.panel_Plugins.Controls.Count; i++)
                {
                    Control control = this.panel_Plugins.Controls[i];
                    if (control is LabelCheckBox)
                    {
                        LabelCheckBox check = control as LabelCheckBox;
                        if (check.Checked)
                        {
                            // initialize plugin, if not initialized
                            LoadPlugin p = check.Tag as LoadPlugin;
                            IEditorPlugin plugin = Engine.Instance.EditorManager.PluginManager.Instantiate(p);
                            plugin.Load();

                            IEditorPluginManager plu = Engine.Instance.EditorManager.PluginManager;
                            plu.PluginTypes.Remove(p);
                        }
                    }
                }
            }
        }
    }
}
